<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\web\Application;
use backend\models\Admin;
header('content-type:text/html;charset=utf8');
class AdminController extends Controller
{
	public $layout = 'main1.php';
	//后台主页
	public function actionDashboard()
	{
		return $this->render('dashboard');
	}
	//角色添加列表
	public function actionAdd()
	{

		$model = new Admin();
    if($model->load($data = Yii::$app->request->post())&&$model->validate())
    {
      // echo "<pre>";
      // print_r($data);exit;
        if($model->save())
        {
          $admin_id = Yii::$app->db->getLastInsertID();
          $where ='';
           foreach ($data['role_id'] as $key => $val) {
            $where.=",($admin_id,$val) ";
           }
           $where = substr($where,1);
           // echo $where;exit;
          if(Yii::$app->db->createCommand("insert into seven_admin_role values $where")->execute())
          {
            $this->redirect(['show']);
          }
        }
    }
    else
    {
      $data = Yii::$app->db->createCommand("select * from seven_role")->queryAll();
      return $this->render('add',['model'=>$model,'data'=>$data]);
    }
   
	}
	//验证码
	public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
             
           'captcha' => [
                  'class' => 'yii\captcha\CaptchaAction',
                  'maxLength' => 5,
                  'minLength' => 5],
        ];
    }

    //角色展示列表
    public function actionShow()
    {

    	$data = Yii::$app->db->createCommand('SELECT admin_id,admin_user from seven_admin')->queryAll();
      foreach ($data as $key => $val) {
      $arr = Yii::$app->db->createCommand('SELECT role_name from seven_role where role_id in ( SELECT role_id from seven_admin_role  where admin_id in (SELECT admin_id from seven_admin where admin_id ='.$val['admin_id'].'))')->queryAll();
        $arr =  array_column($arr,'role_name');
        $data[$key]['role_name'] = implode(',',$arr);
      }
    	return $this->render('show',['data'=>$data]);
    }
    public function actionDelete()
    {
      $admin_id = $_GET['admin_id'];
      if($admin_id ==1)
      { 
      echo "<script>alert('超级管理员您无法删除');location.href='index.php?r=admin/show'</script>";exit();
      }
      //删除用户表
      $admin = Admin::findOne($admin_id);
      
      //删除用户角色关联表
     if($admin->delete()&&Yii::$app->db->createCommand('delete from seven_admin_role where admin_id = '.$admin_id)->execute())
     {
        $this->redirect(['show']);
     }
    }
    //管理员修改
    public function actionUpdate()
    {
      $model = new Admin();
      $admin_id = $_GET['admin_id'];
      if($admin_id ==1)
      { 
      echo "<script>alert('超级管理员您无法修改');location.href='index.php?r=admin/show'</script>";exit();
      }
      if($model->load($data = Yii::$app->request->post())&&$model->validate())
      {
       $model =  Admin::findOne($data['admin_id']);
       $model->admin_pwd = $data['Admin']['admin_pwd'];
       $model->save();
       Yii::$app->db->createCommand("delete from seven_admin_role where admin_id = ".$data['admin_id'])->execute();
      $where ='';
       foreach ($data['role_id'] as $key => $val) {
        $where.=",($admin_id,$val) ";
       }
       $where = substr($where,1);
      if(Yii::$app->db->createCommand("insert into seven_admin_role values $where")->execute())
      {
        $this->redirect(['show']);
      }
      }else
      {
        $arr = admin::findOne($admin_id);
        $info = Yii::$app->db->createCommand("select * from seven_role where role_id in (select role_id from seven_admin_role where admin_id = $admin_id)")->queryAll();
        $info = array_column($info,'role_id');
        $data = Yii::$app->db->createCommand("select * from seven_role")->queryAll();

        return $this->render('update',['model'=>$arr,'data'=>$data,'info'=>$info]);
      }
      
    }

}
function array_column($data,$column)
{
  $arr = array();
  foreach ($data as $key => $val) {
    array_push($arr,$val[$column]);
  }
  return $arr;
}
?>